/************************************************************************
                                                                     
   Xceed Ultimate ListBox for Silverlight                                                                                                                                            
   Copyright (C) 2010 Xceed Software Inc.    
                                                                     
   This program is provided to you under the terms of the GNU General Public  
   License version 2 as published by the Free Software Foundation. 
        
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY, without even the implied warranty of 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License along 
   with this program, if not, write to the Free Software Foundation, Inc., 
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

   This program can be provided to you by Xceed Software Inc. under a
   proprietary commercial license agreement for use in non-Open Source
   projects. Visit Xceed.com to find the commercial edition of 
   Xceed Ultimate ListBox for Silverlight.                                    
                                                                      
 **********************************************************************/

using System;

namespace Xceed.Silverlight.Data
{
  internal class ItemPositionConverterEnumerator<T> : AsyncParentEnumerator<T> where T : RawDataItem
  {
    internal ItemPositionConverterEnumerator(
      IAsyncEnumerator childEnumerator,
      IDataHelper dataHelper )
      : base( childEnumerator )
    {
      if( dataHelper == null )
        throw new ArgumentNullException( "dataHelper" );

      m_dataHelper = dataHelper;
    }


    public override JumpMoveAndFetchAsyncResult BeginJumpMoveAndFetch(
      JumpMoveAndFetchParameters parameters,
      AsyncCallback callback,
      object state )
    {
      this.CheckMoveAndFetchParameters( parameters.ExpectedMoveCount, parameters.ExpectedFetchCount );

      ItemPosition itemPosition = parameters.SourcePosition as ItemPosition;

      // Convert the ItemPosition into a SortPosition.
      if( itemPosition != null && this.Convert( itemPosition ) )
      {
        T item = itemPosition.Item as T;

        if( item == null )
          throw new ArgumentException( "The specified ItemPosition must be a RawDataItem instance.", "parameters" );

        // Find out if the cursor must be placed before or after the item.
        long moveCount = parameters.ExpectedMoveCount;
        int fetchCount = parameters.ExpectedFetchCount;
        bool isHeadingForward = DataUtils.IsQueryingForward( moveCount, fetchCount );
        StartPosition startPosition;

        // The item that is at the ItemPosition must be returned if its available.
        if( moveCount == 0 )
        {
          startPosition = ( isHeadingForward )
                            ? StartPosition.Before
                            : StartPosition.After;
        }
        // The item that is at the ItemPosition must be skipped.
        else
        {
          startPosition = ( isHeadingForward )
                            ? StartPosition.After
                            : StartPosition.Before;

          // There is no need to adjust the move count even if we have skipped
          // the item since it takes one move to move the cursor on the next item.
        }

        SortPosition sortPosition = SortPosition.Create(
                                      m_dataHelper.CompleteSort,
                                      item,
                                      startPosition );

        // Create the new query parameters to use.
        parameters = new JumpMoveAndFetchParameters(
                       sortPosition,
                       moveCount,
                       fetchCount,
                       parameters.ApproximateMove );
      }

      return base.BeginJumpMoveAndFetch( parameters, callback, state );
    }

    protected virtual bool Convert( ItemPosition itemPosition )
    {
      return true;
    }


    #region Private Fields

    private IDataHelper m_dataHelper; //null

    #endregion
  }
}
